{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 序列(包括:列表list,元组tuple,字符串str)\n",
    "\n",
    "# 可索引,可切片,可迭代\n",
    "a = [1,8,5,7,1,8,2]\n",
    "print('\\n',\n",
    "    a[6] , a[-1] ,'\\n', # a[n-1] = a[-1]\n",
    "    a[0] , a[-7] ,'\\n', # a[0] = a[-n]\n",
    "    a[:3] ,'\\n',\n",
    "    a.index(8) ,'\\n',\n",
    ")\n",
    "for i in a[::-1]:       # 序列逆序排列\n",
    "    print(i,end='')\n",
    "print('\\n')\n",
    "for i in reversed(a):   # a序列的倒序\n",
    "    print(i,end='')\n",
    "\n",
    "# 序列类型基本运算符\n",
    "print('\\n\\n', \n",
    "    6 in a  ,'\\n',\n",
    "    6 not in a  ,'\\n',\n",
    "    6 is a  ,'\\n',\n",
    "    a*2,'\\n',\n",
    "    a+[6,8,2,9]\n",
    ")\n",
    "\n",
    "# 序列类型转换函数\n",
    "print('\\n\\n',\n",
    "    list('18571826829'),'\\n', \n",
    "    str(a),'\\n',   \n",
    "    tuple(a),'\\n', \n",
    ")\n",
    "\n",
    "# 序列类型常用内建函数\n",
    "print('\\n\\n', \n",
    "    len(a),'\\n',   # a序列的长度\n",
    "    max(a),'\\n',   # a序列的最大值\n",
    "    sum(a),'\\n',   # a序列的和\n",
    "    sorted(a,reverse=True),'\\n',   # a序列从大到小排列\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 集合\n",
    "a = [1,8,5,7,1,8,2] \n",
    "b = [1,8,8]\n",
    "aset = set(a)\n",
    "bset = set(b)\n",
    "print(a,b)\n",
    "print(aset,bset)\n",
    "print(\n",
    "    a > b ,'\\n',   # 起始元素开始算 a 小于 b\n",
    "    aset > bset    # a '包含' b\n",
    ")      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 集合的关系运算\n",
    "aSet = set('sunrise')\n",
    "bSet = set('sunset')\n",
    "print(\n",
    "    aSet & bSet,'\\n',   # 取交集\n",
    "    aSet | bSet,'\\n',   # 取并集\n",
    "    aSet - bSet,'\\n',   # 取差集(属于a不属于b)\n",
    "    aSet ^ bSet,'\\n\\n',   # 取异或(不同时属于两个集合)\n",
    ")\n",
    "\n",
    "# 集合的内建函数\n",
    "print(\n",
    "    aSet.issubset(bSet),'\\n',               # 是否为子集\n",
    "    aSet.intersection(bSet),'\\n',           # 取并集\n",
    "    aSet.difference(bSet),'\\n',             # 取差集(属于a不属于b)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "5\n6 4\n3 4 5\n"
     ]
    }
   ],
   "source": [
    "# 元组: 元组的元素不可修改,可以拆包,可以按序引用\n",
    "tup = (4,5,6)\n",
    "a,b,c = tup\n",
    "print(b)\n",
    "# 也可轻易改变变量名\n",
    "a,c = c,a\n",
    "print(a,c)\n",
    "# 更高级的拆包\n",
    "d,e,*rest = 1,2,3,4,5\n",
    "print(*rest)   # *表示不想要的变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n小明\n[5, 6, 7, 8, 9]\n[7, 8, 9]\n[7, 8]\n[9, 8, 7, 6, 5]\n小刚\n['小刚']\n[5, 6, 7, 8, 9, 10]\n[5, [1, 2], 7, 9, 10]\n[5, [1, 2]]\n[5, [1, 2], 34]\n"
     ]
    }
   ],
   "source": [
    "### 列表：数据需要【依次放好】时，就像把书从低到高堆起来\n",
    "# 可拓展的容器对象:list列表可变(但是tuple元组不可变,没有多个方法)\n",
    "# 缺点:与字典、集合相比，检查列表中是否包含一个值是非常缓慢的。\n",
    "# 生成列表\n",
    "a = list(range(0,10))\n",
    "print(a)\n",
    "\n",
    "# 偏移量：从0开始\n",
    "list1=['小明',18,(1.70,65)]\n",
    "print(list1[0])\n",
    "\n",
    "#切片：左取右不取\n",
    "list2 = [5,6,7,8,9]\n",
    "print(list2[:])\n",
    "print(list2[2:])\n",
    "print(list2[2:4])\n",
    "\n",
    "# 列表反转\n",
    "print(list2[::-1])\n",
    "#偏移量取到的是列表中的元素，切片则是截取了列表的某部分，所以还是列表\n",
    "students = ['小明','小红','小刚']\n",
    "print(students[2])\n",
    "print(students[2:])\n",
    "\n",
    "# 列表的各个方法:增加元素\n",
    "list2.append(10)   # 将列表作为对象操作\n",
    "list2.sort(reverse=False)       # 列表排序\n",
    "# sorted(list2)    # 该函数并不会修改list2对象\n",
    "print(list2)\n",
    "\n",
    "# 修改元素\n",
    "list2[1]=[1,2]\n",
    "\n",
    "# 指定位置移除元素\n",
    "list2.pop(3)\n",
    "print(list2)\n",
    "\n",
    "# 删除元素\n",
    "del list2[2:]\n",
    "print(list2)\n",
    "\n",
    "# 指定位置插入元素\n",
    "list2.insert(2,34)\n",
    "print(list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表的解析 推导式\n",
    "\n",
    "print(\n",
    "    [x**2 for x in range(10)],'\\n',         # 先执行循环,再将其依次放入列表,中括号不能忘\n",
    "    [x**2 for x in range(10) if x**2<50]    # 加入条件\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表定义的错误示例\n",
    "unit_info = []\n",
    "for i in range(3):\n",
    "    for j in range(3):\n",
    "        unit_info[i][j] = 3\n",
    "print(unit_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 为什么要用字典?\n",
    "name = ['YANG','WANG','SHANG','HAN']\n",
    "salaries = [8,5,2,6]\n",
    "print(salaries[name.index('YANG')])     # 将name作为salaries的索引\n",
    "# 用salaries['name'] 更好些"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# zip函数的使用(多个列表同时遍历) 生成的迭代器第n个元素为各迭代对象组成的元组\n",
    "a = [1,2,3,4,5]\n",
    "b = [6,7,8,9,10]\n",
    "for i,j in zip(a,b):\n",
    "    print(i,j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = ['YANG','WANG','SHANG','HAN']\n",
    "salaries = [8,5,2,6]\n",
    "dict(zip(name,salaries))    ### 常用生成字典的方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 字典：当数据需要【打标签】时，就像图书馆用标签分类\n",
    "#  字典创建和赋值\n",
    "info = [['YANG',8],['WANG',5],['SHANG',2],['HAN',6]]    # 将二维列表转为字典\n",
    "inf = dict(info)\n",
    "inf['key1'] = 'value1'  # 字典赋值\n",
    "inf['key2'] = 'value2'\n",
    "inf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典的方法\n",
    "scores = {'小明':95,'小红':90,'小刚':90}    # 字典中的KEY是无序的\n",
    "scores_2 = {'小明':100,'小红':80,'小紫':92}\n",
    "scores['小妹'] = 92\n",
    "print(\n",
    "    scores['小红'],'\\n',\n",
    "    len(scores)         ,'\\n',\n",
    "    scores.keys()       ,'\\n',\n",
    "    scores.values()     ,'\\n',\n",
    "    scores.items()      ,'\\n',  # 输出plist\n",
    "    scores.update(scores_2),    # 既有更改内容又有新增内容\n",
    "    scores.get('小梦')  ,'\\n', \n",
    "\n",
    ")\n",
    "\n",
    "#一般不做字典和列表相互切换，确定后不变换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#异同点\n",
    "\n",
    "#列表有序，要用偏移量定位；字典无序，便通过唯一的键来取值。\n",
    "students1 = ['小明','小红','小刚']\n",
    "students2 = ['小刚','小明','小红']\n",
    "print(students1 == students2)\n",
    "scores1 = {'小明':95,'小红':90,'小刚':100}\n",
    "scores2 = {'小刚':100,'小明':95,'小红':90}\n",
    "print(scores1 == scores2)\n"
   ]
  },
  {
   "source": [
    "# for 遍历字典\n",
    "scores = {'语文':89, '数学':95, '英语':80}\n",
    "sum_score = 0\n",
    "\n",
    "for i in scores:\n",
    "    print(i)    # i遍历所有键\n",
    "    print(scores[i])    # 所有值\n",
    "for subject,score in scores.items():   #.items()分别遍历字典的键和值\n",
    "    print(subject)\n",
    "    print(score)\n",
    "    \n",
    "    \n",
    "SCORES = {'Jacy':{'语文':89, '数学':95, '英语':80},\n",
    "         'Mary':{'语文':89, '数学':95, '英语':80}}\n",
    "for name in SCORES.values():    #  .values()遍历整个字典的value\n",
    "    print(name)\n",
    "    for subject,score in name.items(): \n",
    "        print(subject+str(score))\n",
    "        \n",
    "# for i in list_foods:\n",
    "#     tag_a = list_foods[i].find('a')    错误示范！  【i为对象】 "
   ],
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 嵌套\n",
    "\n",
    "students = [['小明','小红','小刚','小美'],['小强','小兰','小伟','小芳']]\n",
    "print(students[1][3])\n",
    "\n",
    "scores = {\n",
    "    '第一组':{'小明':95,'小红':90,'小刚':100,'小美':85},\n",
    "    '第二组':{'小强':99,'小兰':89,'小伟':93,'小芳':88}\n",
    "    }\n",
    "print(scores['第二组']['小芳'])\n",
    "\n",
    "students = {\n",
    "    '第一组':['小明','小红','小刚','小美'],\n",
    "    '第二组':['小强','小兰','小伟','小芳']\n",
    "    }\n",
    "print(students['第一组'][3])\n",
    "\n",
    "scores = [\n",
    "    {'小明':95,'小红':90,'小刚':100,'小美':85},\n",
    "    {'小强':99,'小兰':89,'小伟':93,'小芳':88}\n",
    "    ]\n",
    "print(scores[1]['小强'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 循环\n",
    "\n",
    "#【for可遍历字典和列表】\n",
    "dict = {'日本':'东京','英国':'伦敦','法国':'巴黎'}\n",
    "for i in dict:\n",
    "    print(i)\n",
    "print('事情全部办完了！现在留在空房间里的人是谁？')\n",
    "print(i)    #i会逐个接待字典中的每一个【键】\n",
    "print(dict[i])    #提取字典中的【值】\n",
    "\n",
    "#for遍历\n",
    "for i in range(13,17):\n",
    "    print(i)    #取左不取右\n",
    "for i in range(0,10,3):\n",
    "    print(i)\n",
    "\n",
    "#while遍历\n",
    "a = 0\n",
    "while a < 5:\n",
    "    a = a + 1\n",
    "    print(a)\n",
    "    \n",
    "#while放行\n",
    "password = ''  # 变量password用来保存输入的密码\n",
    "while password != '816':\n",
    "    password = input('请尝试输入密码：')\n",
    "print('欢迎回家！')\n",
    "\n",
    "#or循环和whlie循环最大的区别在于【循环的工作量是否确定】\n",
    "#for循环就像空房间依次办理业务，直到把【所有已知的工作做完】才下班。\n",
    "#但while循环就像哨卡放行，【满足条件就一直工作】，直到不满足条件就关闭哨卡。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# while expression:\n",
    "sumJ = 0\n",
    "j = 0\n",
    "while j < 10:\n",
    "    j+=1\n",
    "    sumJ+=j\n",
    "else: # [可选项]正常结束循环(没有终止)就执行else语句\n",
    "    print(sumJ)\n",
    "\n",
    "# for var in vars: [可迭代器包括:字符串/列表/字典]\n",
    "for i in 'python':\n",
    "    print(i,end='')\n",
    "print('\\n',end='')\n",
    "\n",
    "# 跳出当前的循环结构\n",
    "sumJ = 0\n",
    "j = 0\n",
    "while True:\n",
    "    j+=1\n",
    "    sumJ+=j\n",
    "    if j > 9:\n",
    "        break   # 跳出当前循环\n",
    "else:\n",
    "    print('正常结束循环')   # 不执行\n",
    "print(sumJ)\n",
    "\n",
    "# 停止当前循环,进入下一个循环\n",
    "sumJ = 0\n",
    "j = 0\n",
    "while j < 10:\n",
    "    j+=1\n",
    "    if j == 5:\n",
    "        continue    # 跳过当前循环\n",
    "    sumJ+=j\n",
    "print(sumJ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 递归\n",
    "# 斐波拉契数列循环实现\n",
    "def fib(n):\n",
    "    'the nth Fibonacci number'\n",
    "    a,b = 0,1\n",
    "    count = 1\n",
    "    while count < n+1:\n",
    "        a,b = b,a+b\n",
    "        count = count +1\n",
    "    return a\n",
    "\n",
    "# 递归实现斐波拉契数列:执行效率较低,没办法找到循环方法却又明显的递归方法时采用\n",
    "def fibc(n):\n",
    "    'the nth Fibonacci number'\n",
    "    if n==0 or n==1:    # 需要有出口\n",
    "        return n \n",
    "    else:\n",
    "        return fibc(n-1)+fibc(n-2)  # 直到每个值都减到出口时,程序有输出\n",
    "\n",
    "fib(5),fibc(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 遍历时生成迭代器 指针位置\n",
    "lst = [1,2,4,3,5]\n",
    "for x in lst:\n",
    "    if x%2 ==0:\n",
    "        lst.remove(x)      # 当'2'被删去后,后面数据前移;下一次循环时从lst[2]开始\n",
    "print(lst)                 # 遍历后'4'没有被删除\n",
    "\n",
    "lst = [1,2,4,3,5]\n",
    "for x in lst[:]:           # 创建一个lis的浅拷贝\n",
    "    if x%2 ==0:\n",
    "        lst.remove(x)      # 此时删去x并不改变lst[:]\n",
    "print(lst)                 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#作业4.2\n",
    "\n",
    "townee = [\n",
    "    {'海底王国':['小美人鱼','海之王','小美人鱼的祖母','五位姐姐'],\n",
    "     '上层世界':['王子','邻国公主']},\n",
    "     '丑小鸭','坚定的锡兵','睡美人','青蛙王子',\n",
    "    [{'主角':'小红帽','配角1':'外婆','配角2':'猎人'},{'反面角色':'狼'}]\n",
    "    ]\n",
    "print(townee[5][1]['反面角色'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#作业5.2.1\n",
    "students = ['小明','小红','小刚','小美']\n",
    "for i in range(len(students)):\n",
    "     student1 = students[0]    #多了个空格就无法运行    \n",
    "    for i in range(len(students)-1):\n",
    "        students[i] = students[i+1]       \n",
    "    students[len(students)-1] = student1 \n",
    "    print(students)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#优化后\n",
    "students = ['小明','小红','小刚']\n",
    "for i in range(3):\n",
    "    student1 = students[0]\n",
    "    students = students[1:]\n",
    "    students.append(student1)\n",
    "    print(students)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#再次优化\n",
    "students = ['小明','小红','小刚','小美']\n",
    "for i in range(len(students)):\n",
    "    a = students.pop(0)\n",
    "    students.append(a)\n",
    "    print(students)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假的判定\n",
    "\n",
    "if 0 + False :\n",
    "    print('条件都为真')\n",
    "elif []:\n",
    "    print('条件都为真')\n",
    "elif '':\n",
    "    print('条件都为真')\n",
    "else:\n",
    "    print('条件都为假')\n",
    "\n",
    "print(bool(False))\n",
    "print(bool(0))\n",
    "print(bool(''))\n",
    "print(bool(None))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 布尔运算\n",
    "\n",
    "a = 1\n",
    "b = -1\n",
    "list = [0,1,2,3]\n",
    "print(bool(a==1 and b==1))\n",
    "print(bool(a==1 or b==1))\n",
    "print(bool(a in list))\n",
    "print(bool(a not in list))\n",
    "print(bool(not a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 5\n",
    "while i:\n",
    "    print('把这句话打印5遍')\n",
    "    i = i-1\n",
    "# 计算次数减少了（减少了计算i<100,i<99……这个过程）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#if...break的意思是如果满足了某一个条件，就提前结束循环。【这个只能在循环内部使用】\n",
    "while True:    # True常和break配合\n",
    "    t = input('输入密码')\n",
    "    if t == '小龙女':\n",
    "        print('通过')\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#if...continue当某个条件被满足的时候，将跳过之后的代码，直接回到循环的开始。\n",
    "for i in range(5):\n",
    "    print(str(i)+'明日复明日')\n",
    "    if i==3:\n",
    "        continue # 回到循环开头\n",
    "    print('这句话在i等于3的时候打印不出来')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#这三个问题，只有连续答对，才能判定他就是西夏公主的意中人；否则，只要有任何一题错，都要换人从头开始回答问题。\n",
    "while True:\n",
    "    q1 = input('第一问：你一生之中，在什么地方最是快乐逍遥？')\n",
    "    if q1 != '黑暗的冰窖':    #不为真就重来\n",
    "        continue\n",
    "    print('答对了，下面是第二问：')\n",
    "    q2 = input('你生平最爱之人，叫什么名字？')\n",
    "    if q2 != '梦姑':\n",
    "        continue\n",
    "    print('答对了，下面是第三问：')\n",
    "    q3 = input('你最爱的这个人相貌如何？')\n",
    "    if q3 == '不知道':     #为真就结束\n",
    "        break\n",
    "print('都答对了，你是虚竹。')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pass 跳过\n",
    "a = int(input('请输入一个整数:'))\n",
    "if a >= 100:\n",
    "    pass\n",
    "else:\n",
    "    print('你输入了一个小于100的数字')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#for...else \n",
    "for i in range(5):\n",
    "    a = int(input('请输入0来结束循环，你还有'+str(5-i)+'次机会:'))\n",
    "    if a == 0:\n",
    "        print('你触发了break语句，循环结束，导致else语句不会生效。')    \n",
    "        break\n",
    "else:\n",
    "    print('密码已被锁定')\n",
    "#循环正常结束【没有碰到break语句】，就执行循环后面的else语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#猜数游戏\n",
    "mind_number = 24\n",
    "for i in range(3):\n",
    "    check = int(input('还有'+str(3-i)+'次机会说出你猜的数：'))\n",
    "    if check < mind_number :\n",
    "        print('太小了')\n",
    "    elif check > mind_number:\n",
    "        print('太大了')\n",
    "    else :\n",
    "        print('猜对了')\n",
    "        break\n",
    "else :\n",
    "    print('你失败了！')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#作业6.1\n",
    "Criminal = {}\n",
    "n = 0\n",
    "while True:\n",
    "    n += 1\n",
    "    a = int(input('罪犯A是否认罪（1为认罪/0为不认罪）：'))\n",
    "    b = int(input('罪犯B是否认罪（1为认罪/0为不认罪）：'))\n",
    "    Criminal['第'+str(n)+'组'] = [a,b]\n",
    "    if a+b == 0:\n",
    "        print('两人都抵赖，则两人各判10年')\n",
    "    elif a+b == 1:\n",
    "        if a :\n",
    "            print('一个认罪一个抵赖，则认罪的A判1年，抵赖的B判20年')\n",
    "        else :\n",
    "            print('一个认罪一个抵赖，则认罪的B判1年，抵赖的A判20年')\n",
    "    elif a+b == 2:\n",
    "        print('两人都认罪，各判3年')\n",
    "        break\n",
    "    else :\n",
    "        print('输入错误')\n",
    "print(Criminal)\n",
    "for i in range(n):\n",
    "    print('第'+str(i+1)+'组结果分别是'+str(Criminal['第'+str(i+1)+'组']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#作业6.2\n",
    "\n",
    "n = 0\n",
    "list_answer = []\n",
    "while True:\n",
    "    n += 1\n",
    "    a = input('A，你认罪吗？请回答认罪或者不认：')\n",
    "    b = input('B，你认罪吗？请回答认罪或者不认：')\n",
    "    list_answer.append([a,b])  # 用列表嵌套的方式来存放实验者的选择，也可用元组或字典。\n",
    "    if a == '认罪' and b == '认罪':\n",
    "        print('两人都得判10年，唉')\n",
    "    elif a == '不认' and b == '认罪':\n",
    "        print('A判20年，B判1年，唉')\n",
    "    elif a == '认罪' and b == '不认':\n",
    "        print('A判1年，B判20年')\n",
    "    else:\n",
    "        print('都判3年，太棒了')\n",
    "        break\n",
    "print('第' + str(n) + '对实验者选了最优解。')\n",
    "for i in range(n):\n",
    "    # 注意数据类型的转换，以及计数起点的不同（0和1）\n",
    "    print('第' + str(i+1) + '对实验者的选择是：' + str(list_answer[i]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 7 小游戏大学问\n",
    "import time \n",
    "import random\n",
    "win1 = 0\n",
    "win2 = 0\n",
    "for i in range(3):\n",
    "    time.sleep(1.5)  # 让局与局之间有较明显的有时间间隔\n",
    "    blood1 = random.randint(100,150)\n",
    "    blood2 = random.randint(100,150)\n",
    "    power1 = random.randint(30,50)\n",
    "    power2 = random.randint(30,50)\n",
    "    print('【玩家】\\n血量：'+str(blood1)+'\\n攻击：'+str(power1))  # 自定义玩家角色的血量和攻击，用换行符'\\n'来优化视觉\n",
    "    print('------------------------')  # 辅助功能，起到视觉分割的作用，让代码的运行结果更清晰\n",
    "    time.sleep(1) \n",
    "    print('【敌人】\\n血量：'+str(blood2)+'\\n攻击：'+str(power2))\n",
    "    print('------------------------')\n",
    "    while (blood1 >= 0) and (blood2 >= 0):\n",
    "    #and两边的条件分别用括号括起，是一种习惯，方便阅读\n",
    "        time.sleep(1) \n",
    "        blood2 = blood2 - power1\n",
    "        print('你发起了攻击，【敌人】剩余血量'+str(blood2)) \n",
    "        if blood2 <= 0:\n",
    "            time.sleep(1) \n",
    "            print('敌人死翘翘了，你赢了！') # 打印结果\n",
    "            win1 += 1\n",
    "            break\n",
    "        blood1 = blood1 - power2\n",
    "        print('敌人向你发起了攻击，【玩家】剩余血量'+str(blood1)) \n",
    "        if blood1 <= 0:\n",
    "            time.sleep(1) \n",
    "            win2 += 1\n",
    "            print('你死翘翘了，敌人赢了！') # 打印结果\n",
    "            break\n",
    "    print('------------------------')\n",
    "    if win1 == 2:\n",
    "        print('你三局两胜，赢了')\n",
    "        break\n",
    "    if win2 == 2:\n",
    "        print('敌人三局两胜，赢了')\n",
    "        break\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#格式化字符串 ('%d' % () )   和   ( '{}'.format() )  两种方法\n",
    "print('【玩家】\\n'+'血量：'+str(100)+'\\n攻击：'+str(50)) \n",
    "print('【玩家】\\n血量：%s \\n攻击：%s'%(100,50))\n",
    "print('【玩家】\\n血量：{} \\n攻击：{}'.format(100,50))\n",
    "#%后面有一个字母s，这是一个类型码，用来控制数据显示的类型。%s就表示先占一个字符串类型的位置。\n",
    "\n",
    "lucky = 8\n",
    "print('我的幸运数字是%d' % lucky)\n",
    "print('我的幸运数字是%s' % lucky)\n",
    "\n",
    "print('我的幸运数字是%s' % '小龙女的生日816')\n",
    "# print('我的幸运数字是%d' % '小龙女的生日816')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#最后成果\n",
    "import time\n",
    "import random\n",
    "\n",
    "player_victory = 0\n",
    "enemy_victory = 0\n",
    "\n",
    "while True:\n",
    "    for i in range(1,4):\n",
    "        time.sleep(1.5)\n",
    "        print('  \\n——————现在是第 %s 局——————' % i)\n",
    "        #对比之前：(' \\n——————现在是第'+str(i)+'局——————')\n",
    "        player_life = random.randint(100,150)\n",
    "        player_attack = random.randint(30,50)\n",
    "        enemy_life = random.randint(100,150)\n",
    "        enemy_attack = random.randint(30,50)\n",
    "\n",
    "        print('【玩家】\\n血量：%s\\n攻击：%s' % (player_life,player_attack))\n",
    "        print('------------------------')\n",
    "        time.sleep(1)\n",
    "        print('【敌人】\\n血量：%s\\n攻击：%s' % (enemy_life,enemy_attack))\n",
    "        print('-----------------------')\n",
    "        time.sleep(1)\n",
    "\n",
    "        while player_life > 0 and enemy_life > 0:\n",
    "            player_life = player_life - enemy_attack \n",
    "            enemy_life = enemy_life - player_attack\n",
    "            print('你发起了攻击，【玩家】剩余血量%s' % player_life)\n",
    "            print('敌人向你发起了攻击，【敌人】的血量剩余%s' % enemy_life)\n",
    "            print('-----------------------')\n",
    "            time.sleep(1.2)\n",
    "\n",
    "        if player_life > 0 and enemy_life <= 0:\n",
    "            player_victory += 1\n",
    "            print('敌人死翘翘了，你赢了！')\n",
    "        elif player_life <= 0 and enemy_life > 0:\n",
    "            enemy_victory += 1\n",
    "            print('悲催，敌人把你干掉了！')\n",
    "        else:\n",
    "            print('哎呀，你和敌人同归于尽了！')\n",
    "\n",
    "    if player_victory > enemy_victory :\n",
    "        time.sleep(1)\n",
    "        print('\\n【最终结果：你赢了！】')\n",
    "    elif enemy_victory > player_victory:\n",
    "        print('\\n【最终结果：你输了！】')\n",
    "    else: \n",
    "        print('\\n【最终结果：平局！】')\n",
    "    answer = input('是否要再来一盘：')\n",
    "    if answer == '是':\n",
    "        pass \n",
    "    else:\n",
    "        print('游戏结束！')\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#作业6.3\n",
    "movies = {\n",
    "'妖猫传':['黄轩','染谷将太'],\n",
    "'无问西东':['章子怡','王力宏','祖峰'],\n",
    "'超时空同居':['雷佳音','佟丽娅'],\n",
    "}\n",
    "# movie = input('输入你想要的电影：')\n",
    "# print('%s出演了电影%s'%(movies[movie],movie))\n",
    "\n",
    "actor = input('你想查询哪个演员？')\n",
    "for movie in movies:  # 用 for 遍历字典\n",
    "    print(movie)\n",
    "    print(movies[movie])    #帮助理解遍历字典用法\n",
    "    actors = movies[movie]  # 读取各个字典的主演表\n",
    "    if actor in actors:     #若数据在列表内，则...\n",
    "        print(actor + '出演了电影' + movie)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.7.4 64-bit ('base': conda)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "interpreter": {
   "hash": "2a7e95a32014fc1ccf24626d45a98c6e7b4373277259c22f47a91d487fc3e8a5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}